package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.IFloorService;
import com.zzyl.nursing.service.IWeChatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author ljc
 * @version V1.0
 * @date 2025-03-03 20:33
 */
@Service
public class IWeChatServiceImpl implements IWeChatService {
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Override
    public String getOpenId(String code) {
        HashMap<String, Object> params = getParam();
        params.put("js_code", code);
        String json = HttpUtil.get(REQUEST_URL, params);
        JSONObject jsonObject = JSONUtil.parseObj(json);
        if (ObjectUtil.isNotEmpty(jsonObject)) {
            if (ObjectUtil.isNotEmpty(jsonObject) && ObjectUtil.isEmpty(jsonObject.getStr("errcode"))) {
                return jsonObject.get("openid").toString();
            }
        }

        throw new BaseException("获取openid失败");
    }

    private HashMap<String, Object> getParam() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("appid", appid);
        params.put("secret", secret);
        return params;
    }

    @Override
    public String getPhoneNumber(String detailCode) {
        String token = getToken();
        String url =     PHONE_REQUEST_URL + token;
        HashMap<String, Object> params =new HashMap<>();
        params.put("code", detailCode);
        String  json = HttpUtil.post(url,JSONUtil.toJsonStr(params));
        JSONObject jsonObject = JSONUtil.parseObj(json);
        if (ObjectUtil.isNotEmpty(jsonObject)&&ObjectUtil.isNotEmpty(jsonObject.getStr("errcode"))){
            return jsonObject.getJSONObject("phone_info").get("phoneNumber").toString();
        }
        throw new BaseException("获取openid失败");

    }

    /**
     *  获取token
     */
    private String getToken() {
        HashMap<String, Object> params = getParam();
        String result  = HttpUtil.get(TOKEN_URL, params);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 判断接口响应是否出错
        if(ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }

        String token = jsonObject.getStr("access_token");

        return token;

    }
}
